package 计算BMI指数;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class BMIClient extends Application {
    DataOutputStream toServer = null;
    DataInputStream fromServer = null;

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 380, 150, Color.WHITE);

        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(5);
        gridpane.setVgap(5);
        ColumnConstraints column1 = new ColumnConstraints(100);
        ColumnConstraints column2 = new ColumnConstraints(50, 150, 300);
        column2.setHgrow(Priority.ALWAYS);
        gridpane.getColumnConstraints().addAll(column1, column2);

        Label fNameLbl = new Label("身高");
        TextField fNameFld = new TextField();
        Label lNameLbl = new Label("体重");
        TextField lNameFld = new TextField();

        Button saveButt = new Button("提交");
        VBox vbox = new VBox();
        vbox.getChildren().addAll(fNameFld, lNameFld, saveButt);


        GridPane.setHalignment(fNameLbl, HPos.RIGHT);
        gridpane.add(fNameLbl, 0, 0);


        GridPane.setHalignment(lNameLbl, HPos.RIGHT);
        gridpane.add(lNameLbl, 0, 1);


        GridPane.setHalignment(fNameFld, HPos.LEFT);
        gridpane.add(fNameFld, 1, 0);


        GridPane.setHalignment(lNameFld, HPos.LEFT);
        gridpane.add(lNameFld, 1, 1);


        GridPane.setHalignment(saveButt, HPos.RIGHT);
        gridpane.add(saveButt, 1, 2);


        BorderPane mainPane = new BorderPane();
        TextArea ta = new TextArea();
        mainPane.setCenter(new ScrollPane(ta));
        mainPane.setRight(new ScrollPane(vbox));

        root.setCenter(gridpane);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Client_BMI");
        primaryStage.show();

        saveButt.setOnAction(e -> {
            try {
                double height = Double.parseDouble(fNameFld.getText().trim());
                double weight = Double.parseDouble(lNameFld.getText().trim());

                toServer.writeDouble(height);
                toServer.writeDouble(weight);
                toServer.flush();

                double BMI = fromServer.readDouble();

                ta.appendText("身高 is" + height + "\n");
                ta.appendText("体重 is" + weight + "\n");
                ta.appendText("BMI is" + BMI + '\n');

            } catch (IOException ex) {
                System.err.println(ex);
            }
        });
        try {
            Socket socket = new Socket("localhost", 8000);
            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            lNameFld.appendText(ex.toString() + '\n');
        }

    }


    public static void main(String[] args) {
        launch(args);
    }

}
